<?php

class Model_Bookmark extends Ac_Model
{

   const TYPE = 7;
   protected static  $_collection = 'bookmark';

    /**
     *
     * @var array define the requirements for this model
     */
    protected static $_requirements = array(
     //   'em'  => array('Required', 'Validator:EmailAddress'), //Email
    );

    protected static $_transcript = array(
        '_id'  => '_id',
        'us'   => 'user',
        'uid'  => 'uniqid',
        'ls'   => 'listing',
        'c_dt' => 'created_at',
    );

    public function __construct($data = array(), $config = array())
	{
        $config['collection'] = 'bookmark';
        self::ensureIndex(array('c_dt'));
        self::ensureIndex(array('us'=>1));
        self::ensureIndex(array('us'=>1, 'ls'=>1));
        parent::__construct($data, $config);
    }

    public function __get($name)
    {
        if ($name == 'listing') {
            return Model_Listing::find($this->ls);
        } elseif($name == 'user') {
            return Model_User::find($this->us);
        } else {
            return parent::__get($name);
        }
    }

    public static function addListing($id)
    {
        if (empty($id)) {
            return false;
        }
        if (Zend_Registry::isRegistered('loggedin_user')) {
            $user_id = Zend_Registry::get('loggedin_user')->_id;
            $h = Model_Bookmark::one(array('listing' => $id, 'user'=> $user_id));
        } else {
            //generate a unique id and put it in a cookie if it doesn 't exists
            if (empty($_COOKIE['ac_bookmark'])) {
                $uniqid = uniqid('', true);
                setcookie('ac_bookmark', $uniqid, time()+86400*365, "/");
                $_COOKIE['ac_bookmark'] = $uniqid;
            } else {
                $uniqid = $_COOKIE['ac_bookmark'];
            }
            $h = Model_Bookmark::one(array('listing' => $id, 'uid'=> $uniqid));
        }
        

        
        if (empty($h)) {
            $h = new self;
            $h->number_views = 1;
            $h->listing   = $id;
            if (!empty($user_id)) {
                $h->user   = $user_id;
            } else {
                $h->uniqid = $uniqid;
            }
        }

        $h->created_at = new MongoDate(time());
        $h->save();
        return true;
    }
	
	public static function clearList() 
    {
        if (Zend_Registry::isRegistered('loggedin_user')) {
            $user_id = Zend_Registry::get('loggedin_user')->_id;
            if (!empty($_COOKIE['ac_bookmark'])) {
                $uniqid = $_COOKIE['ac_bookmark'];
                self::remove(array('uid' => $uniqid));
            }
            if (!empty($user_id)) {
                self::remove(array('us' => $user_id));
            }            
            return self::all(array('us' => $user_id));
        } else {
            //generate a unique id and put it in a cookie if it doesn 't exists
            if (!empty($_COOKIE['ac_bookmark'])) {
                $uniqid = $_COOKIE['ac_bookmark'];
                self::remove(array('uid' => $uniqid));
                return self::all(array('uid' => $uniqid));
            }
        }
    }
	
	public static function storeCompany($id)
    {
        /* if (empty($id)) {
            return false;
        }
        if (Zend_Registry::isRegistered('loggedin_user')) {
            $user_id = Zend_Registry::get('loggedin_user')->_id;
            $h = Model_Bookmark::one(array('listing' => $id, 'user'=> $user_id));
        } else {
            //generate a unique id and put it in a cookie if it doesn 't exists
            if (empty($_COOKIE['ac_bookmark'])) {
                $uniqid = uniqid('', true);
                setcookie('ac_bookmark', $uniqid, time()+86400*365, "/");
            } else {
                $uniqid = $_COOKIE['ac_bookmark'];
            }
            $h = Model_Bookmark::one(array('listing' => $id, 'uid'=> $uniqid));
        }
        

        
        if (empty($h)) {
            $h = new self;
            $h->number_views = 1;
            $h->listing   = $id;
            if (!empty($user_id)) {
                $h->user   = $user_id;
            } else {
                $h->uniqid = $uniqid;
            }
        }

        $h->created_at = new MongoDate(time());
        $h->save();
        return true; */
    }
	
    public static function getList()
    {
        if (Zend_Registry::isRegistered('loggedin_user')) {
            $user_id = Zend_Registry::get('loggedin_user')->_id; 
            if (!empty($_COOKIE['ac_bookmark'])) {
                $uniqid = $_COOKIE['ac_bookmark'];
                $res = self::update(array('uid' => $uniqid), array('$set' => array('us' => $user_id)), array('multiple' => true));
            }
            return self::all(array('us' => $user_id));
        } else {
            //generate a unique id and put it in a cookie if it doesn 't exists
            if (!empty($_COOKIE['ac_bookmark'])) {
                $uniqid = $_COOKIE['ac_bookmark'];
                return self::all(array('uid' => $uniqid));
            }
        }
        return false;
    }
}
